package com.baizhi.reflect2;

import java.lang.reflect.Field;

/**
 * 通过反射获取成员变量
 */
public class FieldDemo {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, NoSuchFieldException {
        Class<?> aClass = Class.forName("com.baizhi.reflect2.Person");

        //获取多个成员变量,只能获取public修饰的
        Field[] fields = aClass.getFields();
        for (Field field : fields) {
            System.out.println(field);
        }

        System.out.println("======================");
        //获取一个属性
        Field age = aClass.getField("age");
        System.out.println(age);

        System.out.println("~~~~~~~~~~~~~~~~~~~~~~");

        //获取所有的属性
        Field[] fs = aClass.getDeclaredFields();
        for (Field f : fs) {
            System.out.println(f);
        }
        System.out.println("-----------------");

        Field address = aClass.getDeclaredField("address");
        System.out.println(address);

    }
}
